function inits() {
    $("#jqGrid").Grid({
        url: '../energyDevRepair/list',
        colModel: [
            {label: '产线', name: 'devProductName', index: 'DEV_PRODUCT_NAME'},
            {label: '工序号', name: 'devProceNo', index: 'DEV_PROCE_NO', width: 60},
            {label: '工序', name: 'devProceName', index: 'DEV_PROCE_NAME'},
            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 120},
            {label: '故障代码', name: 'faultTypeCode', index: 'TYPE_NAME', width: 80},
            {label: '故障类型', name: 'typeName', index: 'TYPE_NAME', width: 80},
            {label: '维修开始时间', name: 'repairStartDate', index: 'REPAIR_START_DATE', width: 100},
            {label: '故障时长(分)', name: 'faultNum', index: 'FAULT_NUM', width: 70},
            {label: '维修人', name: 'repairUsername', index: 'REPAIR_USER', width: 50},
            {
                label: '换件记录', width: 60, sortable: false,
                formatter: function (value, col, row) {
                    return "<button title='' class='btn btn-outline btn-info' onclick='vm.addReplacePartRecord(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {
                label: '维修详情', width: 60, sortable: false,
                formatter: function (value, col, row) {
                    return "<button title='' class='btn btn-outline btn-info' onclick='vm.getInfo(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: '设备类型', name: 'devKind', hidden: true}
        ]
    });
}

$(function () {
    inits();
    // 加载列表页的设备故障类型树
    vm.getFaultTree();
    // 初始化表单页用到的设备故障类型树
    vm.getFaultTypeTree();
});

//定义树
var faultTree;
//定义树
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: function () {
            let node = faultTree.getSelectedNodes();
            if (node[0].typePid != '0') {
                vm.q.type = node[0].id;
                vm.query()
            }
        }
    }
};

// 表单页用到的设备类型故障树
var faultTree2;
var setting2 = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: 70
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        }
    }
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        typeName: "",
        devMaintenRepair: {
            id: '',
            devId: '', // 设备ID
            devName: '',// 设备名称
            devCode: '', // 设备编码
            repairEndDate: '',
            repairStartDate: '',
            typeName: '', // 故障类型名称
            type: '', // 故障ID
            faultTypeCode: '', // 故障代码
            recordDescr: '',// 故障描述
            reason: '', //  原因分析
            solution: '', // 解决办法
            remark: '' // 备注
        },
        deviceInfoList: {},
        ruleValidate: {
            devId: [
                {required: true, message: '设备编号不能为空', trigger: 'blur'}
            ]
        },
        repairStartRange: [], // 维修开始查询时间区间
        q: {
            workshopId: '', // 车间ID
            name: '', // 设备名称
            spareName: '', // 备件名称
            spareSpec: '', // 备件规格
            type: '', // 故障类型
            beginTime: '',
            endTime: '',
        },
        replaceSpareListCols: [
            {title: '备件编码', key: 'spareCode'},
            {title: '名称', key: 'name'},
            {title: '规格', key: 'spec'},
            {title: '更换数目', key: 'num'}
        ],
        replaceSpareList: [],
        workshopList: [] // 所属车间下拉option列表
    },
    created: function () {
        // 加载车间列表
        this.loadWorshopList();
    },
    methods: {
        loadWorshopList: function () {
            var _this = this;
            Ajax.request({
                url: '../sys/dept/listByDeptLevel?deptLevel=2',
                async: true,
                successCallback: data => {
                    this.workshopList = data.list;
                }
            });
        },
        // 关联备件换件记录
        addReplacePartRecord: function (ticketId) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 维修换件记录',
                content: '../spare/sparepartconsumrecord.html?businessId=' + ticketId + '&type=ENERGY_REPAIR&devKind=2'
            })
        },
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html?devKind=2',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        vm.devMaintenRepair.devCode = res.devCode;
                        vm.devMaintenRepair.devName = res.devName;
                        vm.devMaintenRepair.devId = res.id;
                        layer.close(index);
                    }
                }
            })
        },
        query: function () {
            vm.reload();
        },
        getFaultTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../faulttype/select?category=1",
                async: true,
                successCallback: function (r) {
                    var tree = r.faultList;
                    faultTree = $.fn.zTree.init($("#faultTree"), setting, tree);
                    faultTree.expandNode(faultTree.getNodeByParam("id", '70', null));
                }
            });
        },
        getFaultTypeTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../faulttype/select?category=1",
                async: true,
                successCallback: function (r) {
                    var tree = r.faultList;
                    faultTree2 = $.fn.zTree.init($("#faultTypeTree"), setting2, tree);
                    faultTree2.expandNode(faultTree2.getNodeByParam("id", '70', null));
                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择故障类型",
                area: ['300px', '450px'],
                content: jQuery("#faultTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = faultTree2.getSelectedNodes();
                    //选择上级部门
                    vm.devMaintenRepair.type = node[0].id;
                    vm.devMaintenRepair.faultTypeCode = node[0].faultCode;
                    vm.devMaintenRepair.typeName = node[0].typeName;
                    layer.close(index);
                }
            });
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devMaintenRepair = {
                id: '',
                repairStartDate: '',
                repairEndDate: '',
                devId: '', // 设备ID
                devName: '',// 设备名称
                devCode: '', // 设备编码
                type: '', // 故障ID
                typeName: '', // 故障类型名称
                faultTypeCode: '', // 故障代码
                recordDescr: '',// 故障描述
                reason: '', //  原因分析
                solution: '', // 解决办法
                remark: '' // 备注
            };
            //查询设备列表
            Ajax.request({
                url: "../deviceinfo/deviceInfoAll",
                async: true,
                successCallback: function (r) {
                    vm.deviceInfoList = r.list;
                }
            })
            vm.getFaultTypeTree();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            Ajax.request({
                url: "../deviceinfo/deviceInfoAll",
                async: false,
                successCallback: function (r) {
                    vm.deviceInfoList = r.list;
                }
            });
            vm.getFaultTypeTree();
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var start = vm.devMaintenRepair.repairStartDate;
            var end = vm.devMaintenRepair.repairEndDate;
            if (start >= end) {
                alert('请输入正确的起止时间');
            } else {
                if (vm.devMaintenRepair.repairStartDate) {
                    vm.devMaintenRepair.repairStartDate = gmt2DateTimeStr(vm.devMaintenRepair.repairStartDate)
                }
                if (vm.devMaintenRepair.repairEndDate) {
                    vm.devMaintenRepair.repairEndDate = gmt2DateTimeStr(vm.devMaintenRepair.repairEndDate)
                }
                var url = vm.devMaintenRepair.id == (null || '') ? "../energyDevRepair/addRepairRecord" : "../energyDevRepair/update";
                Ajax.request({
                    url: url,
                    params: JSON.stringify(vm.devMaintenRepair),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            }
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../energyDevRepair/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            layer.closeAll();
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../energyDevRepair/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.showList = false;
                    vm.title = "维修记录详情";
                    vm.devMaintenRepair = r.devMaintenRepair;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            if (this.q.beginTime) {
                this.q.beginTime = gmt2DateStr(this.q.beginTime);
            }
            if (this.q.endTime) {
                this.q.endTime = gmt2DateStr(this.q.endTime);
            }
            // $("#jqGrid").setGridWidth($(window).width());
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: this.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            this.q = {
                workshopId: '',
                name: '', // 设备名称
                spareName: '', // 备件名称
                spareSpec: '', // 备件规格
                type: '', // 故障类型
                beginTime: '',
                endTime: '',
            };
            this.repairStartRange = [];
            this.reload();
            faultTree.cancelSelectedNode();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getByDevCode: function (id) {
            if (!id) {
                return;
            }
            Ajax.request({
                url: "../deviceinfo/info/" + id,
                type: 'POST',
                successCallback: function (r) {
                    var deviceInfo = r.deviceInfo;
                    vm.devMaintenRepair.devId = deviceInfo.id;
                    vm.devMaintenRepair.devCode = deviceInfo.devCode;
                    vm.devMaintenRepair.devName = deviceInfo.devName;
                }
            });
        },
        handleRepairStartRangeChange(dateVal,dateType){
            this.q.beginTime = dateVal[0];
            this.q.endTime = dateVal[1];
        }
    }
});
